package com.bnz.order.controller;


import com.bnz.common.entity.PayVo;
import com.bnz.common.entity.TbPayLog;
import com.bnz.order.entity.Cart;
import com.bnz.order.entity.TbAddress;
import com.bnz.order.entity.TbOrder;
import com.bnz.order.service.AddressService;
import com.bnz.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AddressService addressService;
    //1. 欢迎页面
    @GetMapping({"/", "/getOrderInfo.html"})
    public String getOrderInfo(HttpSession session, Model model) {
        // 1.1 首先的到登录名
        String name = (String) session.getAttribute("name");

        // 1.2 根据登录名得到收件人地址
        List<TbAddress> addressList  = addressService.getAddressByName(name);
        // 1.3 根据登录名查询购物车列表
        List<Cart> cartList = orderService.getCartListByName(name);
        // 1.4 将购物车和收件人地址放到model中以便模板的使用
        model.addAttribute("addressList", addressList);
        model.addAttribute("cartList",cartList);

        return "getOrderInfo";
    }

    // 2.订单添加
    @PostMapping("/order/add")
    public String addOrder(HttpSession session, TbOrder tbOrder, Model model) {
        //2.1 得到登录名
        String name = (String) session.getAttribute("name");
        //2.2 添加订单
        orderService.addOrder(name, tbOrder);
        //2.3 从redis中取得支付日志
        TbPayLog payLog = orderService.getPayLogFromRedis(name);
        //2.4 放到model
        model.addAttribute("payLog",payLog);

        // 添加成功后跳转到支付页面
        return "pay";
    }

    //3. 查看支付页面
    @GetMapping("/pay")
    public String payPage(){

        return "pay";
    }
    //4. 显示支付页面

}
